var index = parent.layer.getFrameIndex(window.name);
$("#roles-add").click(function(){
	$("#roles-add").attr("disabled", true);
	var domParent = parent;
	title     = $("#title").val();
	status    = $("#status").val();
	
	if(title == "") {
		layer.tips('请填写角色名字！', $("#title"), {
			tips: [1, '#d9534f'],
			time: 4000
		});
		$("#roles-add").removeAttr("disabled");
		return;
	}
	if(status == "") {
		layer.tips('请选择角色状态！', $("#status"), {
			tips: [1, '#d9534f'],
			time: 4000
		});
		$("#roles-add").removeAttr("disabled");
		return;
	}
	var uri  = $("form[name=roles-add]").attr("action")+"?r="+Math.random();
	var data = $("form[name=roles-add]").serialize();
	ajaxPost(uri, $("#roles-add"), data, function(){
		domParent.$(".link-popup").removeAttr("disabled");
		domParent.location.reload();
		domParent.layer.close(index);
	});
});
$("#roles-edit").click(function(){
	$("#roles-edit").attr("disabled", true);
	var domParent = parent;
	title     = $("#title").val();
	status    = $("#status").val();
	
	if(title == "") {
		layer.tips('请填写角色名字！', $("#title"), {
			tips: [1, '#d9534f'],
			time: 4000
		});
		$("#roles-edit").removeAttr("disabled");
		return;
	}
	if(status == "") {
		layer.tips('请选择角色状态！', $("#status"), {
			tips: [1, '#d9534f'],
			time: 4000
		});
		$("#roles-edit").removeAttr("disabled");
		return;
	}
	var uri  = $("form[name=roles-edit]").attr("action")+"?r="+Math.random();
	var data = $("form[name=roles-edit]").serialize();
	ajaxPost(uri, $("#roles-edit"), data, function(){
		domParent.$(".link-popup").removeAttr("disabled");
		domParent.location.reload();
		domParent.layer.close(index);
	});
});

$(document).ready(function() {
	if(selAuth != "") {
		var aryAuth = selAuth.split(",");
		for(var i = 0; i < aryAuth.length; i++) {
			$("input[value="+aryAuth[i]+"]").parent().attr("class", "checked");
			$("input[value="+aryAuth[i]+"]").prop("checked", true);
		}
	}
	$(".check-all").on("click", function(){
		var container = $(this).attr("data-to");
		var s = $(this).is(":checked");
		$(this).parent().attr("class", !s?"":"checked");
		$(this).prop("checked",s);
		$(container).find("input[type=checkbox]").parent().attr("class", !s?"":"checked");
		$(container).find("input[type=checkbox]").prop("checked", s);
	});
});

$("#roles-auths").click(function(){
	$("#roles-auths").attr("disabled", true);
	domParent = parent;
	var uri  = $("form[name=roles-auths]").attr("action") + "?r="+Math.random();
	var data = $("form[name=roles-auths]").serialize();
	ajaxPost(uri, $("#agent-auths"), data, function(){
		domParent.$(".link-popup").removeAttr("disabled");
		domParent.location.reload();
		domParent.layer.close(index);
	});
});
